﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace syc.Sale
{
    public partial class CaiGou_FuKuan_Tongji : System.Web.UI.Page
    {
       private  Decimal sum1 = 0;
       private Decimal sum2 = 0;
       private Decimal sum3 = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            String sql = @"select 应付金额.合同编号,应付金额.名称,应付金额.应付金额,SUM(采购付款登记.本次付款) as 已付金额,(应付金额.应付金额-SUM(采购付款登记.本次付款))as 未付金额,应付金额.签订日期
            from 应付金额  inner join 采购付款登记 on 应付金额.ID=采购付款登记.合同ID group by 应付金额.合同编号,应付金额.名称,应付金额.应付金额,应付金额.签订日期";
            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                this.SqlDataSource3.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
            }
            this.SqlDataSource3.SelectCommand = (string)this.ViewState["SelectCommand"];                
        }
       
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string temp = this.FaPiaoHao.Text.Trim().Replace("'", "");
            string Sql = "";
            Sql = "[应付金额].[合同编号] like '%" + temp + "%'";
            if (this.GongHuoDanWei.Text != "0")
            {
                Sql += " and [应付金额].[名称]='" + this.GongHuoDanWei.SelectedItem.Text + "'";
            }
            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                Sql += " and [应付金额].[签订日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                Sql += " and [应付金额].[签订日期]<='" + Dt.ToShortDateString() + "'";
            }
            this.SqlDataSource3.SelectCommand = @"select 应付金额.合同编号,应付金额.名称,应付金额.应付金额,SUM(采购付款登记.本次付款) as 已付金额,(应付金额.应付金额-SUM(采购付款登记.本次付款))as 未付金额,应付金额.签订日期
            from 应付金额  inner join 采购付款登记 on 应付金额.ID=采购付款登记.合同ID group by 应付金额.合同编号,应付金额.名称,应付金额.应付金额,应付金额.签订日期 having " + Sql;

            this.ViewState["SelectCommand"] = this.SqlDataSource3.SelectCommand;


        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView2.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView2.PageIndex > 0) this.GridView2.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView2.PageIndex < this.GridView2.PageCount - 1) this.GridView2.PageIndex++;
                        break;
                    case "++":
                        this.GridView2.PageIndex = this.GridView2.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView2.PageCount)
                    {
                        this.GridView2.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView2.PageIndex + 1).ToString();
            }
        }



        protected void GridView2_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView2.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView2.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView2.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView2.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView2.PageIndex == this.GridView2.PageCount - 1 || this.GridView2.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView2.PageIndex > this.GridView2.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }
        }

        protected void GridView2_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView2_DataBound(null, null);
        }
        protected void Button_New_Click(object sender, EventArgs e)
        {
            Response.Redirect("CaiGou_FuKuan_In.aspx");

        }

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {            
            if (e.Row.RowIndex >= 0)
            {
                sum1 += Convert.ToDecimal(e.Row.Cells[3].Text);
                sum2 += Convert.ToDecimal(e.Row.Cells[4].Text);
                sum3 += Convert.ToDecimal(e.Row.Cells[5].Text);
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[2].Text = "合    计：";
                e.Row.Cells[3].Text = sum1.ToString();
                e.Row.Cells[4].Text = sum2.ToString();
                e.Row.Cells[5].Text = sum3.ToString();
            }
        }
        
    }
}
